package com.wonyesoft.wuliu.acticity;

import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.SearchManager.OnCancelListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.utils.DistanceUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.FileUtils;
import com.wonyesoft.wuliu.acticity.utils.PhotoUtils;
import com.wonyesoft.wuliu.acticity.utils.StringUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.CarType;
import com.wonyesoft.wuliu.model.CargoInfo;
import com.wonyesoft.wuliu.view.cpbutton.PayDialog;
import com.wonyesoft.wuliu.view.cpbutton.PayDialog.OnCustomDialogListener;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class AddCargoLastActivity extends BaseActivity implements
		OnClickListener, OnGetGeoCoderResultListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right, cargoinfo_cargotypetxt,
			cargoinfo_cargovolumetxt, cargoinfo_cargoweighttxt, driver_time,
			cargoinfo_cargocartypetxt, cargoinfo_cargopay1, driver_guige,
			cargoinfo_cargopay2, a11_common, a12_common,
			cargoinfo_cargocartypetxtisback;
	private ImageView head_left, driver_imgone, driver_imgtwo, driver_imgthree,
			addcargo_paytype_img, addcargo_payfang_img, addcargo_huidan_img;
	private EditText driver_carnumber, driver_youhao, driver_leixing,
			driver_long, a8_common;
	private Button driver_baochun, a9_common, a10_common;
	private String passwordString, rePwdString, repwd2String, driverID;
	private RequestParams params;
	private LinearLayout cargoinfo_shrmap, cargoinfo_fhrmap, driver_zaizhong,
			bytxt, bxtxt, hdtxt;
	private RelativeLayout cargoinfo_cargotype, cargoinfo_cargovolume,
			cargoinfo_cargoweight, cargoinfo_cargocartype,
			cargoinfo_cargopaytype1, cargoinfo_cargopaytype2,
			cargoinfo_cargocarisback, addcargo_paytype_layout,
			addcargo_payfang_layout, addcargo_huidan_layout;
	private LocationClient mLocationClient;
	private LocationMode tempMode = LocationMode.Hight_Accuracy;
	private BitmapFactory.Options options = new BitmapFactory.Options();
	private String carInspection, carLicense, carOperationLicense;
	private String tempcoor = "gcj02";
	private String cargoId = null;
	private List<CargoInfo> sourceDateList;
	private List<CarType> sourceCarTypeList;
	private String aString;
	public static Gson gson = new Gson();
	private LatLng pt1;
	private LatLng pt2;
	private GeoCoder mSearch = null;
	private double mi = 0.0;
	private int changeId = 0;
	private String carTypeId;
	private int isBackList = 2;
	private int isBy = 2;
	private int isBX = 2;
	private int intPayType = 0, intOrderType = 0, intCargoType = 2, intCarType = 0,
			intCargoAmount, intCargoWeight, typeMark;
	private String intPrice;
	private int driver_imgone_mark = 0;
	private int driver_imgtwo_mark = 0;
	private int driver_imgthree_mark = 0;

	private static final int DATE_ID = 1;

	private HttpUtils http = new HttpUtils();

	private int selectedFruitIndex = 0;

	private AppContent app;

	private PreferencesCookieStore preferencesCookieStore;

	private SharedPreferences share;

	private String key = "UserInfo";

	private String seachWords = "重庆市";

	private String flowId = null;

	private final String[] arraycargopaytype = new String[] { "线下支付", "在线支付" };

	private final String[] arraycargoisback = new String[] { "有回单", "没有回单" };

	private final String[] arraycargochargetype = new String[] { "到付", "预付" };

	/*
	 * private final String[] arraycargotype = new String[] { "一般","易碎", "易燃",
	 * "易爆" };
	 */
	private final String[] arraycargotype = new String[] { "一般", "易碎" };
	private final String[] arraycargoweight = new String[] { "1吨以下", "3吨以内",
			"5吨以内", "5吨以上" };
	private final String[] arraycargovolme = new String[] { "1立方米以内", "3立方米以内",
			"5立方米以内", "5立方米以上" };
	private final String[] arraycargocartype = new String[] { "小箱车(面包,金杯)","0.5T车", "1T车",
			"2T车", "4T车", "5T车", "8T车" };
	private Bundle bundle2;
	private String[] noticeMsg = new String[] { "无", "无", "无" };
	private final String[] arraycargocarnotice = null;

	private int carTypeIds[] = { 1, 2, 3 ,4,5,6,7};
	private int ownCarTypeId = 0;
	// private SuggestionSearch mSuggestionSearch;
	private EditText c;
	
	private String mTakePicturePath;
	
	// 相册的RequestCode
	public static final int INTENT_REQUEST_CODE_ALBUM = 6;
	// 照相的RequestCode
	public static final int INTENT_REQUEST_CODE_CAMERA = 7;
	// 裁剪照片的RequestCode
	public static final int INTENT_REQUEST_CODE_CROP = 8;
	// 滤镜图片的RequestCode
	public static final int INTENT_REQUEST_CODE_FLITER = 9;

	// 图片在SD卡中的缓存路径
	private static final String IMAGE_PATH = Environment
			.getExternalStorageDirectory().toString()
			+ File.separator
			+ "tczhuser" + File.separator + "Images" + File.separator;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.aj_addcargo);
		app = (AppContent) getApplication();
		preferencesCookieStore = AppContent.getPreferencesCookieStore();
		// 初始化搜索模块，注册事件监听
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);
		intCarType = getIntent().getIntExtra("intCarType", 0);
		if((intCarType = getIntent().getIntExtra("intCarType", 0))==0) {showLongToast("未正确选择车辆！");finish();}
		findIds();
		blindOnclick();

	}

	private void blindOnclick() {
		options.inPreferredConfig = Config.RGB_565;
		head_left.setVisibility(View.VISIBLE);
		head_right.setVisibility(View.VISIBLE);
		title.setText("用户下单");
		head_right.setText("保存");
		a9_common.setText("期望价格");
		http = new HttpUtils();
		head_left.setOnClickListener(this);
		head_right.setOnClickListener(this);
		cargoinfo_shrmap.setOnClickListener(this);
		cargoinfo_fhrmap.setOnClickListener(this);
		cargoinfo_cargotype.setOnClickListener(this);
		// cargoinfo_cargovolume.setOnClickListener(this);
		// cargoinfo_cargoweight.setOnClickListener(this);
		// cargoinfo_cargocartype.setOnClickListener(this);
		// cargoinfo_cargocarisback.setOnClickListener(this);
		// cargoinfo_cargocartypetxtisback.setOnClickListener(this);
		// cargoinfo_cargopaytype1.setOnClickListener(this);
		cargoinfo_cargopaytype2.setOnClickListener(this);
		a10_common.setOnClickListener(this);
		addcargo_huidan_layout.setOnClickListener(this);
		addcargo_paytype_layout.setOnClickListener(this);
		addcargo_payfang_layout.setOnClickListener(this);
		driver_imgone.setOnClickListener(this);
		driver_imgtwo.setOnClickListener(this);
		driver_imgthree.setOnClickListener(this);
	}

	// 实例化IDS
	private void findIds() {
		driver_carnumber = (EditText) findViewById(R.id.driver_carnumber); // 收货人
		driver_youhao = (EditText) findViewById(R.id.driver_youhao);// 收货人联系方式
		driver_time = (TextView) findViewById(R.id.driver_time);// 收货人地址
		driver_leixing = (EditText) findViewById(R.id.driver_leixing);// 发货人
		driver_long = (EditText) findViewById(R.id.driver_long);// 发货人联系方式
		driver_guige = (TextView) findViewById(R.id.driver_guige);// 发货人联系地址
		a8_common = (EditText) findViewById(R.id.a8_common); // 备注
		addcargo_huidan_layout = (RelativeLayout) findViewById(R.id.addcargo_huidan_layout);// 回单
		addcargo_paytype_layout = (RelativeLayout) findViewById(R.id.addcargo_paytype_layout);// 搬运
		addcargo_payfang_layout = (RelativeLayout) findViewById(R.id.addcargo_payfang_layout);// 保险
		// 货物图片一二三
		driver_imgone = (ImageView) findViewById(R.id.driver_imgone);
		driver_imgtwo = (ImageView) findViewById(R.id.driver_imgtwo);
		driver_imgthree = (ImageView) findViewById(R.id.driver_imgthree);
		a9_common = (Button) findViewById(R.id.a9_common);// 议价按钮
		a9_common.setOnClickListener(this);
		a10_common = (Button) findViewById(R.id.a10_common);// 提交资料
		title = (TextView) findViewById(R.id.head_title);// 标题
		head_left = (ImageView) findViewById(R.id.head_left);// 左键
		head_right = (TextView) findViewById(R.id.head_right);// 右键
		cargoinfo_shrmap = (LinearLayout) findViewById(R.id.cargoinfo_shrmap);// 收货人地址监听
		cargoinfo_fhrmap = (LinearLayout) findViewById(R.id.cargoinfo_fhrmap);// 发货人地址监听
		addcargo_paytype_img = (ImageView) findViewById(R.id.addcargo_paytype_img); // 搬运图片
		addcargo_payfang_img = (ImageView) findViewById(R.id.addcargo_payfang_img);// 保险图片
		addcargo_huidan_img = (ImageView) findViewById(R.id.addcargo_huidan_img);// 回单图片
		hdtxt = (LinearLayout) findViewById(R.id.hdtxt);
		bxtxt = (LinearLayout) findViewById(R.id.bxtxt);
		bytxt = (LinearLayout) findViewById(R.id.bytxt);
		cargoinfo_cargopaytype2 = (RelativeLayout) findViewById(R.id.cargoinfo_cargopaytype2);// 选择支付方式
		cargoinfo_cargotype = (RelativeLayout) findViewById(R.id.cargoinfo_cargotype);// 付款方式
		cargoinfo_cargopay2 = (TextView) findViewById(R.id.cargoinfo_cargopay2);
		cargoinfo_cargopay1 = (TextView) findViewById(R.id.cargoinfo_cargopay1);
		/*
		 * cargoinfo_cargocarisback = (RelativeLayout)
		 * findViewById(R.id.cargoinfo_cargocarisback);
		 * cargoinfo_cargocartypetxtisback = (TextView)
		 * findViewById(R.id.cargoinfo_cargocartypetxtisback);
		 * cargoinfo_cargotypetxt = (TextView)
		 * findViewById(R.id.cargoinfo_cargotypetxt); cargoinfo_cargovolumetxt =
		 * (TextView) findViewById(R.id.cargoinfo_cargovolumetxt);
		 * cargoinfo_cargoweighttxt = (TextView)
		 * findViewById(R.id.cargoinfo_cargoweighttxt);
		 * cargoinfo_cargocartypetxt = (TextView)
		 * findViewById(R.id.cargoinfo_cargocartypetxt); cargoinfo_cargopay1 =
		 * (TextView) findViewById(R.id.cargoinfo_cargopay1);
		 * cargoinfo_cargovolume = (RelativeLayout)
		 * findViewById(R.id.cargoinfo_cargovolume); cargoinfo_cargoweight =
		 * (RelativeLayout) findViewById(R.id.cargoinfo_cargoweight);
		 * cargoinfo_cargocartype = (RelativeLayout)
		 * findViewById(R.id.cargoinfo_cargocartype); cargoinfo_cargopaytype1 =
		 * (RelativeLayout) findViewById(R.id.cargoinfo_cargopaytype1);
		 * 
		 * ((AppContent)getApplication()).mLocationResult = driver_time;
		 * //driver_baochun = (Button) findViewById(R.id.driver_baochun);
		 * a11_common = (TextView) findViewById(R.id.a11_common); a12_common =
		 * (TextView) findViewById(R.id.a12_common);
		 */
	}

	// 监听方法
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cargoinfo_cargotype:
			blindAlert(arraycargopaytype, "请选择付款方式", cargoinfo_cargopay1);
			break;
		case R.id.cargoinfo_cargopaytype2:
			blindAlert(arraycargochargetype, "请选择由谁付款", cargoinfo_cargopay2);
			break;
		case R.id.head_left:
			finish();
			break;
		case R.id.cargoinfo_shrmap:
			Intent intent = new Intent(this, AddCargoMapActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.cargoinfo_fhrmap:
			Intent intent2 = new Intent(this, AddCargoMapActivity.class);
			startActivityForResult(intent2, 2);
			break;
		case R.id.a9_common:
			ae();
			break;
		case R.id.a10_common:
		case R.id.head_right:
			checkCargo();
			break;
		// 货物图片1
		case R.id.driver_imgone:
			typeMark = 3;
			System.out.println("driver_imgone" + typeMark);
			if (driver_imgone_mark == 0)
				addImg();
			else if (driver_imgone_mark == 1) {
				ZoomImage(carInspection);
			}
			break;
		// 货物图片2
		case R.id.driver_imgtwo:
			typeMark = 4;
			System.out.println("driver_imgtwo" + typeMark);
			if (driver_imgtwo_mark == 0)
				addImg();
			else if (driver_imgtwo_mark == 1) {
				ZoomImage(carLicense);
			}
			break;
		// 货物图片3
		case R.id.driver_imgthree:
			typeMark = 5;
			System.out.println("driver_imgthree" + typeMark);
			if (driver_imgthree_mark == 0)
				addImg();
			else if (driver_imgthree_mark == 1) {
				ZoomImage(carOperationLicense);
			}
			break;
		// 回单
		case R.id.addcargo_huidan_layout:
			if (isBackList == 2) {
				isBackList = 1;
				addcargo_huidan_img.setImageResource(R.drawable.ly_huidan_img1);
				hdtxt.setVisibility(View.VISIBLE);
			} else if (isBackList == 1) {
				isBackList = 2;
				addcargo_huidan_img.setImageResource(R.drawable.ly_huidan_img);
				hdtxt.setVisibility(View.GONE);
			}
			break;
		// 搬运
		case R.id.addcargo_paytype_layout:
			if (isBy == 2) {
				isBy = 1;
				addcargo_paytype_img.setImageResource(R.drawable.ly_pay_img1);
				bytxt.setVisibility(View.VISIBLE);
			} else if (isBy == 1) {
				isBy = 2;
				addcargo_paytype_img.setImageResource(R.drawable.ly_pay_img);
				bytxt.setVisibility(View.GONE);
			}
			break;
		// 保险
		case R.id.addcargo_payfang_layout:
			if (isBX == 2) {
				isBX = 1;
				addcargo_payfang_img
						.setImageResource(R.drawable.ly_payfang_img1);
				bxtxt.setVisibility(View.VISIBLE);
			} else if (isBX == 1) {
				isBX = 2;
				addcargo_payfang_img
						.setImageResource(R.drawable.ly_payfang_img);
				bxtxt.setVisibility(View.GONE);
			}
			break;
		default:
			break;
		}
	}

	private void ZoomImage(String image) {
		Intent intent = new Intent(this, ImageDetailsActivity.class);
		intent.putExtra("path", image);
		startActivity(intent);

	}

	private void addImg() {

		new AlertDialog.Builder(AddCargoLastActivity.this)
				.setTitle("上传图片")
				.setItems(new String[] { "拍照上传", "上传手机中的照片" },
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = null;
								System.out.println("addImg=mark22-------"
										+ typeMark);

								switch (which) {
								case 0:

									FileUtils.createDirFile(IMAGE_PATH);
									intent = new Intent(
											MediaStore.ACTION_IMAGE_CAPTURE);
									String path = IMAGE_PATH
											+ UUID.randomUUID().toString()
											+ ".jpg";
									File file = FileUtils.createNewFile(path);
									if (file != null) {
										intent.putExtra(
												MediaStore.EXTRA_OUTPUT,
												Uri.fromFile(file));
									}
									startActivityForResult(intent,
											INTENT_REQUEST_CODE_CAMERA);
									mTakePicturePath = path;
									break;

								case 1:

									Intent intent2 = new Intent(
											Intent.ACTION_PICK, null);
									intent2.setDataAndType(
											MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
											"image/*");
									startActivityForResult(intent2,
											INTENT_REQUEST_CODE_ALBUM);
									break;
								}
							}
						})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create().show();

	}

	// alert弹出通用方法
	private void blindAlert(final String[] slearry, String title,
			final TextView txt) {
		Dialog alertDialog = new AlertDialog.Builder(this)
				.setTitle(title)
				.setIcon(R.drawable.ic_launcher)
				.setSingleChoiceItems(slearry, 0,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								selectedFruitIndex = which;
							}
						})
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.out.println(selectedFruitIndex);
						txt.setText(slearry[selectedFruitIndex]);

						if (txt.equals(cargoinfo_cargopay2)) {
							intOrderType = selectedFruitIndex + 1;
						} else if (txt.equals(cargoinfo_cargopay1)) {
							intPayType = selectedFruitIndex + 1;
						}
						selectedFruitIndex = 0;
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		alertDialog.show();

	}

	private void ae() {
		c = new EditText(AddCargoLastActivity.this);
		c.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		c.setHint("请输入您的期望价格");
		new AlertDialog.Builder(this).setTitle("请输入您的意向金额")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						if ((!StringUtils.isEmpty(c.getText().toString()))
								&& Double.valueOf(c.getText().toString()) > 0) {
							intPrice = c.getText().toString();

							a9_common.setText("￥ " + intPrice);
						} else
							showLongToast("未正确输入金额！");
					}
				}).setNegativeButton("取消", null).setView(c).show();
	}

	//验证提交的数据
	private void checkCargo() {
		if(StringUtils.isEmpty(driver_carnumber.getText().toString())){showLongToast("请填写收货人姓名");return;}
		if(StringUtils.isEmpty(driver_youhao.getText().toString())){showLongToast("请填写收货人电话");return;}
		if(StringUtils.isEmpty(driver_time.getText().toString())){showLongToast("请选择收货地址");return;}
		if(StringUtils.isEmpty(driver_leixing.getText().toString())){showLongToast("请填写发货人姓名");return;}
		if(StringUtils.isEmpty(driver_long.getText().toString())){showLongToast("请填写发货人联系方式");return;}
		if(StringUtils.isEmpty(driver_guige.getText().toString())){showLongToast("请选择发货人联系地址");return;}
		if(intPayType ==0){showLongToast("请选择付款方式");return;}
		if(intOrderType==0){showLongToast("请选择支付方式");return;}
		if(StringUtils.isEmpty(intPrice)){showLongToast("请输入期望价格");return;}
		if(intCarType ==0){showLongToast("未选择车辆类型");finish();return;}
		save();
	}
	
	private void save() {
		String url = GagApi.OnlineOrder;
		Map<String, Object> dataMap = new HashMap<String, Object>();
		if (!(StringUtils.isEmpty(flowId))) {
			dataMap.put("flowId", flowId);
			url = GagApi.USERCHANGELIST;
		}
		dataMap.put("sdLinkMan", driver_carnumber.getText().toString());
		dataMap.put("sdMobile", driver_youhao.getText().toString());
		dataMap.put("sdAddress", driver_time.getText().toString());
		dataMap.put("reLinkMan", driver_leixing.getText().toString());
		dataMap.put("reMobile", driver_long.getText().toString());
		dataMap.put("reAddress", driver_guige.getText().toString());
		dataMap.put("payType", intPayType);// 支付方式 在线支付
		dataMap.put("payMent", intOrderType);// 付款方式
		// dataMap.put("carUseTime", 的);// 付款方式
		// dataMap.put("cargoType", intCargoType);// 货物类型
		dataMap.put("carType", intCarType);
		// dataMap.put("cargoId", cargoId);
		// dataMap.put("cargoVolume", intCargoAmount);
		// dataMap.put("cargoWeight", intCargoWeight);
		dataMap.put("isNickBack", isBackList);
		dataMap.put("transPrice", intPrice);
		dataMap.put("totalFee", intPrice);
		dataMap.put("orderType", 1);
		if(!StringUtils.isEmpty(carInspection))
		dataMap.put("imgId", carInspection);
		if(!StringUtils.isEmpty(carLicense))
		dataMap.put("imgTwo", carLicense);
		if(!StringUtils.isEmpty(carOperationLicense))
		dataMap.put("imgThree", carOperationLicense);
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		System.out.println("json:" + json);
		http.configCookieStore(preferencesCookieStore);
		http.send(HttpRequest.HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						System.out.println("进入添加addcargo");
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();
						// System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {
							showLongToast("下单成功，请等待后台审核！");
							// showLongToast(state.getMsg());
							// setListDetails();
							finish();
						} else
							// System.out.println(state.getMsg());
							showLongToast(state.getMsg());
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() + ":" +
						// msg);
						System.out.println(msg);
					}
				});

	}


	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// aString = data.getExtras().getString("addr");
		System.out.println("resultCode:"+resultCode+"requestCode:"+requestCode);
		if (resultCode == 0)
			return;
		
		/*
		 * if(StringUtils.isEmpty(aString = data.getExtras().getString("addr")))
		 * { System.out.println("asting空置"); return;}
		 */

		switch (requestCode) {
		case (1):
			if(!StringUtils.isEmpty(data.getStringExtra("address"))) aString = data.getStringExtra("address");
			driver_time.setText(aString);
			changeId = 1;
			mSearch.geocode(new GeoCodeOption().city("重庆市").address(aString));
			break;
		case (2):
			if(!StringUtils.isEmpty(data.getStringExtra("address"))) aString = data.getStringExtra("address");
			driver_guige.setText(aString);
			changeId = 2;
			mSearch.geocode(new GeoCodeOption().city("重庆市").address(aString));
			break;
		case PhotoUtils.INTENT_REQUEST_CODE_ALBUM:
			System.out.println("0Photoutils进来了");
			if (data == null) {
				return;
			}
			if (resultCode == RESULT_OK) {
				if (data.getData() == null) {
					return;
				}
				if (!FileUtils.isSdcardExist()) {
					showLongToast("SD卡不可用,请检查");
					return;
				}
				Uri uri = data.getData();
				String[] proj = { MediaStore.Images.Media.DATA };
				Cursor cursor = managedQuery(uri, proj, null, null, null);
				if (cursor != null) {
					int column_index = cursor
							.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					if (cursor.getCount() > 0 && cursor.moveToFirst()) {
						String path = cursor.getString(column_index);
						Bitmap bitmap = BitmapFactory.decodeFile(path);
						if (PhotoUtils.bitmapIsLarge(bitmap)) {
							System.out.println("path" + path);
							// mTakePicturePath = path;
							// System.out.println("mTakePicturePath"+mTakePicturePath);
							if (path != null)
								upPz(path, bitmap);
							// this.setUserPhoto(bitmap);

						} else {
							showLongToast("图像过大");
						}
					}
				}
			}
			break;

		case PhotoUtils.INTENT_REQUEST_CODE_CAMERA:
			if (resultCode == RESULT_OK) {
				System.out.println("Photoutils进来了");
				// String path = data.getStringExtra("output");
				if (mTakePicturePath != null) {
					// Bitmap bitmap =
					// BitmapFactory.decodeFile(mTakePicturePath);d
					Bitmap bitmap = BitmapFactory.decodeFile(mTakePicturePath,
							options);
					// BitmapFactory.decodeFile(mTakePicturePath, options)
					if (PhotoUtils.bitmapIsLarge(bitmap)) {
						System.out.println("path");
						upPz(mTakePicturePath, bitmap);
						// this.setUserPhoto(bitmap);

					} else {
						showLongToast("图像过大");
					}
				}
			} else
				showLongToast("获取图片失败");

			break;

		case PhotoUtils.INTENT_REQUEST_CODE_CROP:
			System.out.println("2Photoutils进来了");
			if (resultCode == RESULT_OK) {
				String path = data.getStringExtra("path");
				System.out.println(path);
				if (path != null) {
					Bitmap bitmap = BitmapFactory.decodeFile(path);
					driver_imgone.setImageBitmap(bitmap);
				}
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult result) {
		System.out.println("geo" + "进来了");
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			System.out.println("有问题");
			Toast.makeText(this, "抱歉，未能找到结果", Toast.LENGTH_LONG).show();
			return;
		}
		// 不知道怎么处理两条地理转码同时处理，借用了一下中转ID
		if (changeId == 1)
			pt1 = result.getLocation();
		if (changeId == 2)
			pt2 = result.getLocation();
		if (pt1 != null && pt2 != null) {

		}

	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
		// TODO Auto-generated method stub

	}

	private void upPz(String path, final Bitmap bitmap) {
		showLoadingDialog("货物图片上传中，请稍后...");
		//压缩图片
		/*System.out.println("uppz--path(before):"+path);
		Bitmap bitmap2 = FileUtils.compressImage(BitmapFactory.decodeFile(path)); 
		 System.out.println(Fi);
		path = FileUtils.
        System.out.println("uppz--path(after):"+path);*/
		//FileUtils.saveImgtoPath(path);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		JsonRequest jr = JsonRequest.getInit();
		// dataMap.put("flId", orderListId);
		System.out.println("typeMark:" + typeMark);
		dataMap.put("imgType", typeMark);
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		System.out.println(json);
		params = new RequestParams();
		params.addBodyParameter("file", new File(path));
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(preferencesCookieStore);
		http.send(HttpRequest.HttpMethod.POST, GagApi.UPLOADIMAGE, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						super.onStart();
						
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						dismissLoadingDialog();
						System.out.println("上传货物图片");
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();

						// System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {
							// 存用户登录信息
							// System.out.println(responseInfo.result);
							String c;
							try {
								c = new JSONObject(responseInfo.result)
										.getJSONObject("data").getString(
												"imgName");
								System.out.println("cccccccc:" + c);
								if (typeMark == 3) {
									carInspection = c;
									driver_imgone_mark = 1;
								} else if (typeMark == 4) {
									carLicense = c;
									driver_imgtwo_mark = 1;
								} else if (typeMark == 5) {
									carOperationLicense = c;
									driver_imgthree_mark = 1;
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							setUserPhoto(bitmap);
							// refreshList();

							// System.out.println(jrs.getParam("rows"));

						} else {
							showLongToast(state.getMsg());
							if (state.getCode().equals(State.UNLOGIN)) {
								openActivity(UserLoginActivity.class);
								finish();
							}

						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						System.out.println(msg);
						dismissLoadingDialog();
					}
				});

	}
	
	// 上传凭证之后显示图片

	public void setUserPhoto(Bitmap bitmap) {
		if (bitmap != null) {
			showLongToast("上传货物图片成功！");
			if (typeMark == 3) {
				driver_imgone.setClickable(false);
				driver_imgone.setImageBitmap(bitmap);
				//njtxt.setText("年检已上传");
			} else if (typeMark == 4) {
				driver_imgtwo.setClickable(false);
				driver_imgtwo.setImageBitmap(bitmap);
				//jsztxt.setText("驾驶证已上传");
			} else if (typeMark == 5) {
				driver_imgthree.setClickable(false);
				driver_imgthree.setImageBitmap(bitmap);
				//xsztxt.setText("行驶证已上传");
			}
			
		} else
			showLongToast("未获取到图片");
	}

	public String getTakePicturePath() {
		return mTakePicturePath;
	}

}
